package com.snowRemovingUser.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.snowRemovingUser.domain.TCustomer;
import com.snowRemovingUser.domain.TOrgan;
import com.snowRemovingUser.mapper.TCustomerMapper;
import com.snowRemovingUser.mapper.TOrganMapper;
import com.snowRemovingUser.service.ITCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 除雪防滑系统的客户管理Service业务层处理
 *
 * @author liguang
 * @date 2023-03-31
 */
@Service
public class TCustomerServiceImpl implements ITCustomerService
{
    @Autowired
    private TCustomerMapper tCustomerMapper;

    @Autowired
    private TOrganMapper tOrganMapper;


    /**
     * 查询除雪防滑系统的客户管理
     *
     * @param id 除雪防滑系统的客户管理主键
     * @return 除雪防滑系统的客户管理
     */
    @Override
    public TCustomer selectTCustomerById(Long id)
    {
        return tCustomerMapper.selectTCustomerById(id);
    }

    /**
     * 查询除雪防滑系统的客户管理列表
     *
     * @param tCustomer 除雪防滑系统的客户管理
     * @return 除雪防滑系统的客户管理
     */
    @Override
    public List<TCustomer> selectTCustomerList(TCustomer tCustomer)
    {

        TOrgan tOrgan = new TOrgan();
        List<TOrgan> tOrganizations = tOrganMapper.selectTOrganList(tOrgan);
        Map<Long, TOrgan> TOrganById = tOrganizations.stream().collect(Collectors.toMap(TOrgan::getId, Function.identity()));


        //查询需要分页
        PageDomain pageDomain = TableSupport.getPageDomain();
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize(), pageDomain.getOrderBy());
        List<TCustomer> tCustomers = tCustomerMapper.selectTCustomerList(tCustomer);

        for (TCustomer customer : tCustomers) {

            customer.setOrganName(TOrganById.get(customer.getOrganId()).getOrganName());
            customer.setLoginIp("******");
       }
        //分页
        Page<TCustomer> tCustomersPage =(Page<TCustomer> )tCustomers;
        return tCustomersPage;
    }

    /**
     * 新增除雪防滑系统的客户管理
     *
     * @param tCustomer 除雪防滑系统的客户管理
     * @return 结果
     */
    @Override
    public int insertTCustomer(TCustomer tCustomer)
    {
        tCustomer.setCreateTime(DateUtils.getNowDate());
        tCustomer.setStatus("0");
        tCustomer.setDelFlag("0");
        return tCustomerMapper.insertTCustomer(tCustomer);
    }

    /**
     * 修改除雪防滑系统的客户管理
     *
     * @param tCustomer 除雪防滑系统的客户管理
     * @return 结果
     */
    @Override
    public int updateTCustomer(TCustomer tCustomer)
    {
        tCustomer.setUpdateTime(DateUtils.getNowDate());
        return tCustomerMapper.updateTCustomer(tCustomer);
    }

    /**
     * 批量删除除雪防滑系统的客户管理
     *
     * @param ids 需要删除的除雪防滑系统的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteTCustomerByIds(Long[] ids)
    {
        return tCustomerMapper.deleteTCustomerByIds(ids);
    }

    /**
     * 删除除雪防滑系统的客户管理信息
     *
     * @param id 除雪防滑系统的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteTCustomerById(Long id)
    {
        return tCustomerMapper.deleteTCustomerById(id);
    }
}
